package com.atguigu.gmall.payment.service.impl;

import com.alipay.api.domain.OrderDetailResult;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/9/5 15:17
 * @Version 1.0
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private RabbitService rabbitService;

    @Override
    public void closePayment(Long orderId) {
        //  创建对象
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(PaymentStatus.CLOSED.name());
        //  设置更新条件
        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOrderId,orderId);
        this.paymentInfoMapper.update(paymentInfo,paymentInfoLambdaQueryWrapper);
    }

    @Override
    public void updatePaymentInfoStatus(String outTradeNo, String paymentType, PaymentInfo paymentInfo) {
        //  构建条件
        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getPaymentType,paymentType);
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo);
        //  更新
        this.paymentInfoMapper.update(paymentInfo,paymentInfoLambdaQueryWrapper);
    }

    @Override
    public void updatePaymentInfoStatus(String outTradeNo, String paymentType, HashMap<String, String> paramsMap) {
        //  获取paymentInfo
        PaymentInfo paymentInfoQuery = this.getPaymentInfo(outTradeNo, paymentType);
        if (null == paymentInfoQuery) return;
        //  修改交易记录状态. trade_no callback_time callback_content payment_status
        //  update payment_info set trade_no = ?,callback_time = ? where out_trade_no = ? and payment_type = ?;
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setTradeNo(paramsMap.get("trade_no"));
        paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
        paymentInfo.setCallbackTime(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
        paymentInfo.setCallbackContent(paramsMap.toString());
        //  构建条件
        //        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getPaymentType,paymentType);
        //        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo);
        //        //  第一个参数： 设置修改的数据  第二个参数：设置更新条件
        //        this.paymentInfoMapper.update(paymentInfo,paymentInfoLambdaQueryWrapper);
        this.updatePaymentInfoStatus(outTradeNo,paymentType,paymentInfo);

        //  发送消息给订单，更改订单状态. orderId 或 outTradeNo; paramsMap.get(out_trade_no)
        this.rabbitService.sendMsg(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,paymentInfoQuery.getOrderId());
    }

    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo, String paymentType) {
        //  查询数据
        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo);
        wrapper.eq(PaymentInfo::getPaymentType,paymentType);
        PaymentInfo paymentInfo = this.paymentInfoMapper.selectOne(wrapper);
        return paymentInfo;
    }

    @Override
    public void savePaymentInfo(OrderInfo orderInfo, String paymentType) {
        //  查询交易记录中是否已经存在
        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentInfo::getOrderId,orderInfo.getId());
        wrapper.eq(PaymentInfo::getPaymentType,paymentType);
        PaymentInfo paymentInfoQuery = this.paymentInfoMapper.selectOne(wrapper);
        if (null != paymentInfoQuery) return;

        //  创建paymentInfo 对象
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setUserId(orderInfo.getUserId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
        paymentInfo.setSubject(orderInfo.getTradeBody());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());;
        paymentInfoMapper.insert(paymentInfo);
    }
}
